package com.platform.system.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.constant.Constants;
import com.platform.common.constant.HttpStatus;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.enums.ResultEnum;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.bean.BeanUtils;
import com.platform.common.utils.uuid.SnowflakeIdWorker;
import com.platform.system.domain.dto.device.SpeakerInfoDto;
import com.platform.system.domain.dto.device.SpeakerTypeDto;
import com.platform.system.domain.entity.device.SpeakerInfo;
import com.platform.system.domain.entity.device.SpeakerType;
import com.platform.system.domain.vo.device.SpeakerInfoVo;
import com.platform.system.mapper.device.SpeakerInfoMapper;
import com.platform.system.service.ISysSpeakerInfoService;
import com.platform.system.service.ISysSpeakerTypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.pl.REGON;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Huangkai
 */

@Slf4j
@Service
public class SysSpeakerInfoServiceImpl extends ServiceImpl<SpeakerInfoMapper, SpeakerInfo> implements ISysSpeakerInfoService {

    @Resource
    SpeakerInfoMapper speakerInfoMapper;

    @Resource
    ISysSpeakerInfoService sysSpeakerInfoService;

    @Resource
    ISysSpeakerTypeService sysSpeakerTypeService;

    @Override
    public TableDataInfo selectSpeakerInfoList(SpeakerInfoVo speakerInfoVo) {
        LambdaQueryWrapper<SpeakerInfo> speakerInfoLambdaQueryWrapper = new QueryWrapper<SpeakerInfo>().lambda();
        speakerInfoLambdaQueryWrapper.like(StringUtils.isNotEmpty(speakerInfoVo.getName()), SpeakerInfo::getName, speakerInfoVo.getName());
        speakerInfoLambdaQueryWrapper.orderByDesc(SpeakerInfo::getCreateTime);
        Page<SpeakerInfo> page = new Page<>();
        page.setCurrent(speakerInfoVo.getPageNum());
        page.setSize(speakerInfoVo.getPageSize());
        IPage<SpeakerInfo> speakerInfoIPage = speakerInfoMapper.selectPage(page, speakerInfoLambdaQueryWrapper);
        TableDataInfo rspData = new TableDataInfo();
        List<SpeakerInfo> list =  speakerInfoIPage.getRecords();
        List<SpeakerInfoDto> speakerInfoDtoArrayList = new ArrayList<>();
        if(list != null || list.size() > 0){
            list.forEach(speakerInfo -> {
                SpeakerInfoDto speakerInfoDto = new SpeakerInfoDto();

                DecimalFormat speedFormat = new DecimalFormat("0.00");
                String speedValue = speedFormat.format(speakerInfo.getSpeed());
                DecimalFormat soundVolumeFormat = new DecimalFormat("0.00");
                String soundVolumeValue = soundVolumeFormat.format(speakerInfo.getSoundVolume());

                DecimalFormat soundToneValueFormat = new DecimalFormat("0.00");
                String soundToneValue = soundToneValueFormat.format(speakerInfo.getSoundTone());

                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append(speedValue + " | ");
                stringBuffer.append(soundVolumeValue + " | ");
                stringBuffer.append(soundToneValue);
                speakerInfoDto.setSoundInfo(stringBuffer.toString());
                BeanUtils.copyProperties(speakerInfo, speakerInfoDto);
                speakerInfoDtoArrayList.add(speakerInfoDto);
            });
        }
        rspData.setRows(speakerInfoDtoArrayList);
        rspData.setTotal(speakerInfoIPage.getTotal());
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        return rspData;
    }

    @Override
    public List<SpeakerInfoDto> selectSpeakerInfoListByUidList(List<String> uidList) {
        LambdaQueryWrapper<SpeakerInfo> speakerInfoLambdaQueryWrapper = new QueryWrapper<SpeakerInfo>().lambda();
        speakerInfoLambdaQueryWrapper.in(SpeakerInfo::getUid, uidList);
        List<SpeakerInfo> speakerInfoList = speakerInfoMapper.selectList(speakerInfoLambdaQueryWrapper);
        if(speakerInfoList != null && speakerInfoList.size() > 0){
            String str = JSONArray.toJSONString(speakerInfoList);
            List<SpeakerInfoDto> speakerInfoDtoList = JSONArray.parseArray(str, SpeakerInfoDto.class);
            List<String> styleUidList = speakerInfoDtoList.stream().map(SpeakerInfoDto::getStyleUid).collect(Collectors.toList());
            List<SpeakerType> speakerTypeList = sysSpeakerTypeService.selectSpeakerTypeByUidList(styleUidList);
            Map<String, List<SpeakerType>> speakerTypeMap = speakerTypeList.stream().collect(Collectors.groupingBy(SpeakerType::getUid));
            speakerInfoDtoList.forEach(speakerInfoDto -> {
                if(speakerTypeMap.containsKey(speakerInfoDto.getStyleUid())){
                    SpeakerType speakerType = speakerTypeMap.get(speakerInfoDto.getStyleUid()).get(0);
                    SpeakerTypeDto speakerTypeDto = new SpeakerTypeDto();
                    BeanUtils.copyProperties(speakerType, speakerTypeDto);
                    speakerInfoDto.setSpeakerTypeDto(speakerTypeDto);
                }
            });
            return speakerInfoDtoList;
        }
        return null;
    }

    @Override
    public SpeakerInfoDto selectSpeakerInfo(String uid) {
        LambdaQueryWrapper<SpeakerInfo> speakerInfoLambdaQueryWrapper = new QueryWrapper<SpeakerInfo>().lambda();
        speakerInfoLambdaQueryWrapper.eq(SpeakerInfo::getUid, uid);
        SpeakerInfo speakerInfo = speakerInfoMapper.selectOne(speakerInfoLambdaQueryWrapper);
        if(speakerInfo != null) {
            SpeakerInfoDto speakerInfoDto = new SpeakerInfoDto();
            BeanUtils.copyProperties(speakerInfo, speakerInfoDto);
            return speakerInfoDto;
        }
        return null;
    }

    @Override
    public Long selectCountSpeakerInfoByType(String typeUid) {
        LambdaQueryWrapper<SpeakerInfo> speakerInfoLambdaQueryWrapper = new QueryWrapper<SpeakerInfo>().lambda();
        speakerInfoLambdaQueryWrapper.eq(SpeakerInfo::getStyleUid, typeUid);
        Long selectCount = speakerInfoMapper.selectCount(speakerInfoLambdaQueryWrapper);
        return selectCount;
    }

    @Override
    public AjaxResult updateSpeakerInfoStatus(SpeakerInfoVo speakerInfoVo) {
        SpeakerInfoDto speakerInfoDto = sysSpeakerInfoService.selectSpeakerInfo(speakerInfoVo.getUid());
        if(speakerInfoDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        SpeakerInfo speakerInfo = new SpeakerInfo();
        speakerInfo.setStatus(Constants.NUM_ZERO);
        if(speakerInfoDto.getStatus() == Constants.NUM_ZERO){
            speakerInfo.setStatus(Constants.NUM_ONE);
        }
        LambdaQueryWrapper<SpeakerInfo> statusUpdateWrapper = new QueryWrapper<SpeakerInfo>().lambda();
        statusUpdateWrapper.eq(SpeakerInfo::getUid, speakerInfoVo.getUid());
        speakerInfo.setUpdateTime(new Date());
        speakerInfo.update(statusUpdateWrapper);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult insertSpeakerInfo(SpeakerInfoVo speakerInfoVo) {
        SpeakerInfo speakerInfo = new SpeakerInfo();
        speakerInfo.setUid(SnowflakeIdWorker.getSnowflakeIdWorker());
        speakerInfo.setName(speakerInfoVo.getName());
        speakerInfo.setRemark(speakerInfoVo.getRemark());
        speakerInfo.setSoundTone(speakerInfoVo.getSoundTone());
        speakerInfo.setSpeed(speakerInfoVo.getSpeed());
        speakerInfo.setSoundVolume(speakerInfoVo.getSoundVolume());
        speakerInfo.setStyleUid(speakerInfoVo.getStyleUid());
        speakerInfo.setStatus(Constants.NUM_ZERO);
        speakerInfo.setUpdateTime(new Date());
        speakerInfo.setCreateTime(new Date());
        speakerInfo.insert();
        return AjaxResult.success();
    }

    @Override
    public AjaxResult deleteSpeakerInfo(SpeakerInfoVo speakerInfoVo) {
        LambdaQueryWrapper<SpeakerInfo> speakerInfoLambdaQueryWrapper = new QueryWrapper<SpeakerInfo>().lambda();
        speakerInfoLambdaQueryWrapper.eq(SpeakerInfo::getUid, speakerInfoVo.getUid());
        speakerInfoMapper.delete(speakerInfoLambdaQueryWrapper);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult updateSpeakerInfo(SpeakerInfoVo speakerInfoVo) {
        log.info("修改的信息{}", speakerInfoVo);
        SpeakerInfoDto speakerInfoDto = sysSpeakerInfoService.selectSpeakerInfo(speakerInfoVo.getUid());
        if(speakerInfoDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        LambdaQueryWrapper<SpeakerInfo> speakerInfoLambdaQueryWrapper = new QueryWrapper<SpeakerInfo>().lambda();
        speakerInfoLambdaQueryWrapper.eq(SpeakerInfo::getUid, speakerInfoVo.getUid());
        SpeakerInfo speakerInfo = new SpeakerInfo();
        BeanUtils.copyProperties(speakerInfoVo, speakerInfo);
        speakerInfo.setUpdateTime(new Date());
        speakerInfo.update(speakerInfoLambdaQueryWrapper);
        return AjaxResult.success();
    }
}
